<!--- get-nodes --->

<cfscript>
  function FileSizeFormat(size)
  {
    var returnValue = '';
    var unit = '';
    var showdecimal = true;
    if(ArrayLen(arguments) GTE 2 AND IsBoolean(arguments[2])) showdecimal = arguments[2];
    if(Val(size) LT 1024)
    {
      returnValue = size;
      unit = ' Bytes';
      returnValue = returnValue & unit;
    }
    else if(Val(size) LT 1000000)
    {
      returnValue = Val(size) / 1024;
      unit = ' KB';
      returnValue = NumberFormat(returnValue, '0.00');
      returnValue = returnValue * 10;
      returnValue = Round(returnValue);
      returnValue = returnValue / 10;
      returnValue = returnValue & unit;
    }
    else if(Val(size) LT 1000000000)
    {
      returnValue = Val(size) / 1048576;
      unit = ' MB';
      returnValue = NumberFormat(returnValue, '0.00');
      returnValue = returnValue * 100;
      returnValue = Round(returnValue);
      returnValue = returnValue / 100;
      if(showdecimal)
        returnValue = DecimalFormat(returnValue);
      else
        returnValue = Round(returnValue);
      returnValue = returnValue & unit;
    }
    else
    {
      returnValue = Val(size) / 1073741824;
      unit = ' GB';
      returnValue = NumberFormat(returnValue, '0.00');
      returnValue = returnValue * 1000;
      returnValue = Round(returnValue);
      returnValue = returnValue / 1000;
      if(showdecimal)
        returnValue = DecimalFormat(returnValue);
      else
        returnValue = Round(returnValue);
      returnValue = returnValue & unit;
    }
    return returnValue;
  }

  /**
   * Serialize native ColdFusion objects into a JSON formated string.
   * 
   * @param arg 	 The data to encode. (Required)
   * @return Returns a string. 
   * @author Jehiah Czebotar (jehiah@gmail.com) 
   * @version 1, January 12, 2006 
   */
  function jsonencode(arg) {
      var i=0;
  	var o="";
  	var u="";
  	var v="";
  	var z="";
  	var r="";
  
  	if (isarray(arg)) {
  		o="";
  		for(i=1;i lte arraylen(arg);i=i+1) {
  			try {
  				v = jsonencode(arg[i]);
  				if (o neq "") {
  					o = o & ',';
  				}
  				o = o & v;
  			}
  			catch(Any ex){
  				o=o;
  			}
  		}
  		return '['& o &']';
  	}
  	if (isstruct(arg)) {
  		o = "";
  		if (structisempty(arg)) return "{null}";
  		z = StructKeyArray(arg);
  		for (i=1;i lte arrayLen(z);i=i+1) {
  			try {
  			v = jsonencode(evaluate("arg."&z[i]));
  			} catch(Any err) { WriteOutput("caught an error when trying to evaluate z[i] where i="& i &" it evals to "  & z[i] ); }
  			if (o neq ""){
  				o = o & ",";
  			}
  			o = o & '"'& lcase(z[i]) & '":' & v;
  		} 
  		return '{' & o & '}';
  	}
  
  	if (isobject(arg)) return "unknown";
  
    if(issimplevalue(arg) AND IsBoolean(arg)) return IIf(arg, DE('true'), DE('false'));

  	if (issimplevalue(arg) and isnumeric(arg)) return ToString(arg);
  
  	if (issimplevalue(arg)) return '"' & JSStringFormat(ToString(arg)) & '"';
  
  	if (IsQuery(arg)){
  		o = o & '"RECORDCOUNT":' & arg.recordcount;
  		o = o & ',"COLUMNLIST":'&jsonencode(arg.columnlist);
  		// do the data [].column
  		o = o & ',"DATA":{';
  		// loop through the columns
  		for (i=1;i lte listlen(arg.columnlist);i=i+1){
  			v = "";
  			// loop throw the records
  			for (z=1;z lte arg.recordcount;z=z+1){
  				if (v neq "") {
  					v =v  & ",";
  				}
  				// encode this cell
  				v = v & jsonencode(evaluate("arg." & listgetat(arg.columnlist,i) & "["& z & "]"));
  			}
  			// put this column in the output
  			if (i neq 1) {
  				o = o & ",";
  			}
  			o = o & '"' & listgetat(arg.columnlist,i) & '":[' & v & ']';
  		}
  		// close our data section
  		o = o & '}';
  		// put the query struct in the output
  		return '{' & o & '}';
  	}
  	return "unknown";
  }
</cfscript>

<cfparam name="url.lib" default="">
<cfparam name="url.node" default="">
<cfparam name="form.lib" default="#url.lib#">
<cfparam name="form.node" default="#url.node#">
<cfscript>
  nodes = ArrayNew(1);
  path = '../../yui-ext/';
  if(LCase(form.lib) EQ 'yui')
    path = '../../../';
</cfscript>
<cfif (Len(form.lib) OR Len(form.node)) AND DirectoryExists(ExpandPath(path) & form.node)>
  <cfdirectory name="getdirectory" directory="#ExpandPath(path)##form.node#" sort="name">
  <cfloop query="getdirectory">
    <cfswitch expression="#LCase(type)#">
    <cfcase value="dir">
      <cfscript>
        temp = StructNew();
        temp['id'] = form.node & '/' & name;
        temp['text'] = name;
  //      temp['qtip'] = 'Type: Folder<br />Last Modified: ' & DateFormat(datelastmodified, 'mmm d, yyyy') & ', ' & LCase(TimeFormat(datelastmodified, 'h:mm tt'));
        temp['cls'] = 'folder';
      </cfscript>
    </cfcase>
    <cfcase value="file">
      <cfscript>
        temp = StructNew();
        temp['id'] = form.node & '/' & name;
        temp['text'] = name;
        temp['leaf'] = true;
  //      temp['qtip'] = 'Type: JavaScript File<br />Last Modified: ' & DateFormat(datelastmodified, 'mmm d, yyyy') & ', ' & LCase(TimeFormat(datelastmodified, 'h:mm tt')) & '<br />Size: ' & FileSizeFormat(size);
  //      temp['qtipTitle'] = name;
        temp['cls'] = 'file';
      </cfscript>
    </cfcase>
    </cfswitch>
    <cfscript>
      ArrayAppend(nodes, temp);
      StructDelete(variables, 'temp');
    </cfscript>
  </cfloop>
</cfif>
<cfcontent type="text/html" reset="Yes"><cfoutput>#JSONEncode(nodes)#</cfoutput>
